package com.wenjiang.controller;

import com.wenjiang.common.annotation.RequireToken;
import com.wenjiang.common.annotation.TokenUserId;
import com.wenjiang.common.dto.CommentParams;
import com.wenjiang.common.dto.GoodCommentParams;
import com.wenjiang.common.lang.Result;
import com.wenjiang.common.vo.CommentVo;
import com.wenjiang.entity.MallReply;
import com.wenjiang.service.MallCommentService;
import com.wenjiang.service.MallReplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

@RequestMapping("mallComment")
@RestController
public class MallCommentController {


    @Autowired
    MallCommentService mallCommentService;

    @Autowired
    MallReplyService mallReplyService;

    //获取用户的所有评论
    @RequestMapping("userComment")
    @RequireToken
    public  Object getUserComment(@TokenUserId Long userId){

        return  mallCommentService.getUserComment(userId);
    }

    //給商品添加一条评论
    @RequireToken
    @RequestMapping("addComment")
    public  Object addGoodComment(@RequestBody CommentParams commentParams){
        return  mallCommentService.commentGood(commentParams);
    }


    //获取某个商品的评论
    @RequestMapping("goodComment")
    public Object getGoodComment(@RequestBody GoodCommentParams goodCommentParams){
        return  mallCommentService.getGoodComment(goodCommentParams);
    }



    // 该商品页面评论添加一条回复
    @RequestMapping("addReply")
    @RequireToken
    public  Object addGoodReply(@TokenUserId Long userId,@RequestBody MallReply mallReply){

        // 判断是否自己评论自己
        if (userId.equals(mallReply.getRepliedUserId())){
            return Result.fail("不能自己评论自己!");
        }



        mallReply.setUserId(userId);
        mallReply.setCreatedTime(LocalDateTime.now());
        return  mallReplyService.addReply(mallReply);
    }
}
